package com.boot.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boot.common.vo.MemberResponseVo;
import com.boot.mall.member.feign.CouponFeignService;
import com.boot.mall.member.vo.MemberLoginVo;
import com.boot.mall.member.vo.MemberRegisterVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.boot.mall.member.entity.MemberEntity;
import com.boot.mall.member.service.MemberService;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.R;

import javax.annotation.Resource;
import javax.validation.Valid;


/**
 * 会员
 *
 * @author huihui
 * @email 2565036109@qq.com
 * @date 2021-05-06 19:33:48
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    CouponFeignService couponFeignService;

    // 微博用户第三方注册
    @PostMapping("/auth/weibo/register")
    public R weiBoUserAuthRegister(@RequestBody MemberResponseVo vo){
        MemberResponseVo member = memberService.authRegister(vo);
        return R.ok().put("member",member);
    }

    // 根据用户uid查询数据库中的用户信息
    @GetMapping("/{uid}/memberInfo")
    public R getMemberInfo(@PathVariable("uid") String uid){
        MemberResponseVo member = memberService.getMemberInfo(uid);
        return R.ok().put("member",member);
    }

    // 用户注册
    @PostMapping("/guli/feign/register")
    public R register(@Valid @RequestBody MemberRegisterVo vo, BindingResult result){
        // 对数据再次数据校验
        if (result.hasErrors()){
            return R.error("数据校验出错");
        }
        memberService.registerMember(vo);
        return R.ok();
    }

    // 用户登录
    @PostMapping("/guli/feign/login")
    public R login(@Valid @RequestBody MemberLoginVo vo,BindingResult result){
        if (result.hasErrors()){
            return R.error("数据校验出错");
        }
        String res = memberService.loginMember(vo);
        if (res.equals("success")){
            // 这步没必要
            MemberEntity memberEntity = memberService.getOne(new QueryWrapper<MemberEntity>().eq("username", vo.getLoginAccount()));
            MemberResponseVo memberRegisterVo = new MemberResponseVo();
            BeanUtils.copyProperties(memberEntity,memberRegisterVo);
            System.out.println(memberRegisterVo);
            return R.ok().put("member",memberRegisterVo);
        }else {
            return R.error(res);
        }
    }

    @GetMapping("/getCoupons")
    public R test(){
        MemberEntity member = new MemberEntity();
        member.setNickname("张三");
        R coupons = couponFeignService.memberCoupons();
        return R.ok().put("name",member).put("coupons",coupons);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils pageUtils = memberService.pageMember(params);
        return R.ok().put("page",pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
